//
//  BaseHttpTool.m
//  TJProperty
//
//  Created by Remmo on 15/6/24.
//  Copyright (c) 2015年 bocweb. All rights reserved.
//

#import "BaseHttpTool.h"
#import "AFNetworking.h"
#import "AFAppDotNetAPIClient.h"

@implementation BaseHttpTool


+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    // 1.获得请求管理者
    AFHTTPSessionManager *session = [AFAppDotNetAPIClient sharedClient];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", nil];
    // 2.发送POST请求
    [session GET:url parameters:params progress:^(NSProgress *uploadProgress) {
        
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    // 1.获得请求管理者
    AFHTTPSessionManager *session = [AFAppDotNetAPIClient sharedClient];
    
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", nil];
    //  放到请求头里面
    /*
    [session.requestSerializer setValue:@"1" forHTTPHeaderField:@"platform"];
    NSString *token = [AccountTool sharedAccountTool].account.token;
    if (token) {
        [session.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    }else{
         [session.requestSerializer setValue:@"379b3f1cc0c8d" forHTTPHeaderField:@"token"];
    }
     */
    // 2.发送POST请求
    [session POST:url parameters:params progress:^(NSProgress *uploadProgress) {
        
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (failure) {
            failure(error);
        }
    }];
}



@end
